package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.mapper.BookMarksMapper;
import org.example.mapper.BookMarksTagsMapper;
import org.example.mapper.UserMapper;
import org.example.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class BookMarksServiceImpl implements org.example.service.BookMarksService {
    @Autowired
    private BookMarksMapper bookMarksMapper;
    @Autowired
    private BookMarksTagsMapper bookMarksTags;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBookMarks(BookMarks bookMarks) {
        bookMarksMapper.addBookMarks(bookMarks);
        List<Integer> tags = bookMarks.getTags();
        if (tags != null && !tags.isEmpty()) {
            for (int tagId : tags) {
                bookMarksTags.addBookMarkTags(bookMarks.getId(), tagId);
            }
        } else {
            bookMarksTags.addBookMarkTags(bookMarks.getId(), 0);
        }
    }
    @Override
    public PageBean bookMarksList(Integer page, Integer size, Integer userId,
                                  String keywords, Integer asterisk, Integer groupId, Integer tagId, String begin, String end) {
        PageHelper.startPage(page, size);
        List<BookMarks> bookMarks = bookMarksMapper.bookMarksList(userId, keywords, asterisk, groupId, tagId, begin, end);
        Page<BookMarks> p = (Page<BookMarks>) bookMarks;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public BookMarks bookMarkDetail(Integer id) {
        List<Integer> tagIds = bookMarksTags.tagIdList(id);
        BookMarks bookMarks = bookMarksMapper.bookMarkDetail(id);
        bookMarks.setTags(tagIds);
        return bookMarks;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBookMarks(List<Integer> ids) {
        bookMarksMapper.delBookMarks(ids);
//        bookMarksTags.deleteByBookMarkId(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editBookMarks(BookMarks bookMarks) {
        bookMarksMapper.editBookMarks(bookMarks);
        List<Integer> newTags = bookMarks.getTags();
        List<Integer> oldTags = bookMarksTags.tagIdList(bookMarks.getId());
        if (!oldTags.isEmpty()) {
            bookMarksTags.deleteByTagIds(bookMarks.getId(), oldTags);
        }
                if (newTags != null && !newTags.isEmpty()) {

                    for (int tagId : newTags) {
                        bookMarksTags.addBookMarkTags(bookMarks.getId(), tagId);
                    }
                } else {
                    bookMarksTags.addBookMarkTags(bookMarks.getId(), 0);
                }

    }

    @Override
    public void addComment(Comment comment) {
        bookMarksMapper.addComment(comment);
    }

    @Override
    public void delComment(Comment comment) {
        bookMarksMapper.delComment(comment);
    }

    @Override
    public List<Comment> getCommentsByBookmarkId(Integer bookmarkId) {
        List<Comment> comments = bookMarksMapper.getCommentsByBookmarkId(bookmarkId);
        comments.forEach(item -> {
            Integer userId = item.getUserId();
            User user = userMapper.getUserById(userId);
            String username = user.getUsername();
            item.setUsername(username);
        });
        return comments;
    }

    @Override
    public void addLike(Like like) {
        bookMarksMapper.addLike(like);
    }

    @Override
    public void cancelLike(Like like) {
        bookMarksMapper.cancelLike(like);
    }

    @Override
    public Integer getLikeCountByBookmarkId(Integer bookmarkId) {
        return bookMarksMapper.getLikeCountByBookmarkId(bookmarkId);
    }

    @Override
    public Integer getLikeStatus(Like like) {
        return bookMarksMapper.getLikeStatus(like);
    }

    public static boolean isEqualIgnoreOrder(List<Integer> list1, List<Integer> list2) {
        List<Integer> sortedList1 = new ArrayList<>(list1);
        List<Integer> sortedList2 = new ArrayList<>(list2);
        Collections.sort(sortedList1);
        Collections.sort(sortedList2);
        return sortedList1.equals(sortedList2);
    }
}
